//
//  XLHomeIndexCell.m
//  industry
//
//  Created by xl on 2020/11/25.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLHomeIndexCell.h"
#import "XLHomeIndexModel.h"

@interface XLHomeIndexCell ()

@property (nonatomic, strong) XLView *bgView;
@property (nonatomic, strong) XLLabel *titleLB;
@property (nonatomic, strong) XLLabel *infoLB;
@property (nonatomic, strong) XLLabel *indexLB;
@property (nonatomic, strong) XLImageView *iconIG;
@property (nonatomic, strong) XLImageView *circleIG;

@end

@implementation XLHomeIndexCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.bgView];
        [_bgView addSubview:self.circleIG];
        [_bgView addSubview:self.titleLB];
        [_bgView addSubview:self.infoLB];
        [_bgView addSubview:self.indexLB];
        [_bgView addSubview:self.iconIG];
        
        [self makeConstraints];
    }
    return self;
}

- (void)makeConstraints {
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentView);
    }];
    
    [_circleIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.bgView);
        make.size.mas_equalTo(CGSizeMake(54, 59));
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(10);
        make.top.equalTo(self.bgView).offset(15);
    }];
    
    [_infoLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLB);
        make.top.equalTo(self.titleLB.mas_bottom);
    }];
    
    [_iconIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(3);
        make.bottom.equalTo(self.bgView).offset(-6);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    
    [_indexLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconIG.mas_top);
        make.left.equalTo(self.bgView).offset(10);
        make.right.equalTo(self.bgView).offset(-10);
    }];
}

#pragma mark - Public
- (void)loadCellData:(XLHomeIndexModel *)model index:(NSInteger)index {
    _titleLB.text = model.indicatorsName;
    if (model.data) {
        _infoLB.text = @"(万)";
    }else{
        _infoLB.text = @"(暂未开放)";
    }
    
    if (model.hideData) {
        // 隐藏数据，以***代替；默认用3个，数据非空用数据长度
        NSInteger length = 3;
        if (!ISEmptyString(model.data)) {
            length = model.data.length;
        }
        _indexLB.text = @"***";
    } else {
        // 正常显示数据
        [self.indexLB setNumberOfLines:2];
        [self.indexLB setText:ISEmptyString(model.data) ? @"- -":[self formateData:model.data]];
//        _indexLB.text =  ;
    }
    _bgView.backgroundColor = (index%2 == 0) ? [UIColor blueBgColor]:[UIColor colorWithHex:0x0C98D3];
}

-(NSString *)formateData:(NSString *)data {
    
    data = [NSString stringWithFormat:@"%.2f", (data.doubleValue / 10000)];
    
    NSArray * array = [data componentsSeparatedByString:@"."];
    NSString * str = array.firstObject;
    NSMutableString * strm = [NSMutableString stringWithString:str];
    int y = 0;
    for (int i = 4; i <= str.length; i += 3) {
        [strm insertString:@"," atIndex:(strm.length - i + 1 - y)];
        y++;
    }
    if (array.count > 1) {
        [strm appendFormat:@".%@",array.lastObject];
    }
    return  strm;
}

#pragma mark - setter/getter
- (XLView *)bgView {
    if (!_bgView) {
        _bgView = [XLView new];
        _bgView.layer.cornerRadius = 12.0f;
        _bgView.layer.masksToBounds = YES;
        _bgView.backgroundColor = [UIColor blueBgColor];
    }
    return _bgView;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        _titleLB.textColor = [UIColor whiteFontColor];
    }
    return _titleLB;
}

- (XLLabel *)infoLB {
    if (!_infoLB) {
        _infoLB = [XLLabel new];
        _infoLB.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        _infoLB.textColor = [UIColor whiteFontColor];
    }
    return _infoLB;
}

- (XLLabel *)indexLB {
    if (!_indexLB) {
        _indexLB = [XLLabel new];
        _indexLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _indexLB.textColor = [UIColor whiteFontColor];
    }
    return _indexLB;
}

- (XLImageView *)iconIG {
    if (!_iconIG) {
        _iconIG = [XLImageView new];
        _iconIG.image = XLIMAGE(@"jxbg");
    }
    return _iconIG;
}

- (XLImageView *)circleIG {
    if (!_circleIG) {
        _circleIG = [XLImageView new];
        _circleIG.image = XLIMAGE(@"zbtybg");
    }
    return _circleIG;
}
@end
